/*
 * @Author: 0x9DEFA478
 * @Date: 2021-12-09 21:09:51
 * @LastEditTime: 2021-12-19 17:20:20
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#ifndef __IP_MPU_H_
#define __IP_MPU_H_
#include "stm32h7xx.h"


#define vIP_MPU_AP_NoAccess                                       0x0U
#define vIP_MPU_AP_PRW                                            0x1U
#define vIP_MPU_AP_PRW_UR                                         0x2U
#define vIP_MPU_AP_FullAccess                                     0x3U
#define vIP_MPU_AP_PRO                                            0x5U
#define vIP_MPU_AP_PRO_URO                                        0x6U

#define vIP_MPU_Size_32B                                          4U
#define vIP_MPU_Size_64B                                          5U
#define vIP_MPU_Size_128B                                         6U
#define vIP_MPU_Size_256B                                         7U
#define vIP_MPU_Size_512B                                         8U
#define vIP_MPU_Size_1KB                                          9U
#define vIP_MPU_Size_2KB                                          10U
#define vIP_MPU_Size_4KB                                          11U
#define vIP_MPU_Size_8KB                                          12U
#define vIP_MPU_Size_16KB                                         13U
#define vIP_MPU_Size_32KB                                         14U
#define vIP_MPU_Size_64KB                                         15U
#define vIP_MPU_Size_128KB                                        16U
#define vIP_MPU_Size_256KB                                        17U
#define vIP_MPU_Size_512KB                                        18U
#define vIP_MPU_Size_1MB                                          19U
#define vIP_MPU_Size_2MB                                          20U
#define vIP_MPU_Size_4MB                                          21U
#define vIP_MPU_Size_8MB                                          22U
#define vIP_MPU_Size_16MB                                         23U
#define vIP_MPU_Size_32MB                                         24U
#define vIP_MPU_Size_64MB                                         25U
#define vIP_MPU_Size_128MB                                        26U
#define vIP_MPU_Size_256MB                                        27U
#define vIP_MPU_Size_512MB                                        28U
#define vIP_MPU_Size_1GB                                          29U
#define vIP_MPU_Size_2GB                                          30U
#define vIP_MPU_Size_4GB                                          31U

//失能区域
#define oIP_MPU_DisableRegion(Region) \
do{\
  MPU->RNR=Region;\
  MPU->RBAR=0;\
  MPU->RASR=0;\
}while(0)

//使能区域
#define oIP_MPU_EnableRegion(Region,Address,XN,AP,TEX,C,B,S,SRD,Size) \
do{\
  MPU->RNR=Region;\
  MPU->RBAR=Address;\
  MPU->RASR=\
  ((XN<<MPU_RASR_XN_Pos)&MPU_RASR_XN_Msk)|\
  ((AP<<MPU_RASR_AP_Pos)&MPU_RASR_AP_Msk)|\
  ((TEX<<MPU_RASR_TEX_Pos)&MPU_RASR_TEX_Msk)|\
  ((C<<MPU_RASR_C_Pos)&MPU_RASR_C_Msk)|\
  ((B<<MPU_RASR_B_Pos)&MPU_RASR_B_Msk)|\
  ((S<<MPU_RASR_S_Pos)&MPU_RASR_S_Msk)|\
  ((SRD<<MPU_RASR_SRD_Pos)&MPU_RASR_SRD_Msk)|\
  ((Size<<MPU_RASR_SIZE_Pos)&MPU_RASR_SIZE_Msk)|\
  MPU_RASR_ENABLE_Msk;\
}while(0)

//使能区域为StronglyOrdered
#define oIP_MPU_EnableRegion_StronglyOrdered(Region,Address,XN,AP,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x0U,0U,0U,0U,SRD,Size)

//使能区域为Device 不共享 (外设区默认属性就是这个)
#define oIP_MPU_EnableRegion_Device(Region,Address,XN,AP,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x2U,0U,0U,0U,SRD,Size)

//使能区域为Device 共享
#define oIP_MPU_EnableRegion_Device_Share(Region,Address,XN,AP,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x0U,0U,1U,0U,SRD,Size)

//配置为普通内存 不使用Cache
#define oIP_MPU_EnableRegion_Normal_NoCache(Region,Address,XN,AP,S,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x1U,0U,0U,S,SRD,Size)

//配置为普通内存 Cache透写
#define oIP_MPU_EnableRegion_Normal_WT(Region,Address,XN,AP,S,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x0U,1U,0U,S,SRD,Size)

//配置为普通内存 Cache回写
#define oIP_MPU_EnableRegion_Normal_WB(Region,Address,XN,AP,S,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x0U,1U,1U,S,SRD,Size)

//配置为普通内存 Cache回写 并且写分配
#define oIP_MPU_EnableRegion_Normal_WBWA(Region,Address,XN,AP,S,SRD,Size) \
oIP_MPU_EnableRegion(Region,Address,XN,AP,0x1U,1U,1U,S,SRD,Size)


#endif //__IP_MPU_H_
